<script lang="ts" setup>
import type { DataType } from '../../types';

import { useVbenModal } from '@vben/common-ui';

import { message } from 'ant-design-vue';
import { $t } from '@vben/locales';
import { useVbenForm } from '#/adapter/form';
import { postLinks, putLink } from '#/api/admin/social';

const [Form, formApi] = useVbenForm({
  handleSubmit: onSubmit,
  schema: [
    {
      component: 'Input',
      dependencies: {
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: ['id'],
      },
      fieldName: 'id',
      label: 'ID',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'name',
      label: $t('page.common.Name'),
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'url',
      label: 'Url',
      rules: 'required',
    },
    {
      component: 'Select',
      defaultValue: '_blank',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
        options: [
          {
            label: $t('admin.newWindow')+'(_blank)',
            value: '_blank',
          },
          {
            label: $t('admin.currentWindow')+'(_self)',
            value: '_self',
          },
          {
            label: $t('admin.parentWindow')+'(_parent)',
            value: '_parent',
          },
          {
            label: $t('admin.mainWindow')+'(_top)',
            value: '_top',
          },
        ],
      },

      fieldName: 'target',
      label: $t('admin.Open Method'),
    },
    {
      component: 'Upload',
      defaultValue: '',
      componentProps: {},
      fieldName: 'image',
      label: $t('page.common.Image'),
    },
    {
      component: 'Upload',
      defaultValue: '',
      componentProps: {},
      fieldName: 'image1',
      label: $t('page.common.Image')+'(social)',
    },
  
  ],
  showDefaultActions: false,
});
const [Modal, formModalApi] = useVbenModal({
  fullscreenButton: false,
  onCancel() {
    formModalApi.close();
  },
  onConfirm: async () => {
    await formApi.validateAndSubmitForm();
  },
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const values: DataType = formModalApi.getData();
      if (values) {
        formApi.setValues(values);
      }
    }
  },
});

async function onSubmit(values: Record<string, any>) {
  function showMsg(msg: string) {
    message.success(msg);
    formModalApi.setData({ isUpdata: true });
    formModalApi.close();
  }
  if (values.id) {
    const { msg } = await putLink(values);
    showMsg(msg);
  } else {
    const { msg } = await postLinks(values);
    showMsg(msg);
  }
}
</script>
<template>
  <Modal class="w-3/5">
    <Form />
  </Modal>
</template>

<style lang="scss" scoped></style>
